<?php
/**
 * Created by Silent Night.
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/9/28
 * Time: 9:19
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class News extends Controller
{
    private $_model;
    private $title_field = [

    ];
    public $pagesize = 10;

    public function _initialize()
    {
        $this->_model = new \app\admin\model\News();
        $this->assign('LoginUserInfo', $this->LoginUserInfo);

        //获取当前用户的权限信息
        $this->assign('ruleLists', $this->LoginUserInfo['CurrPower']);
    }

    public function shangData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }

        $res = $this->_model->updateData(array('stats' => 1), $id);

        if ($res || $res === 0) {


            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }


    }

    public function xiaData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }


        $res = $this->_model->updateData(array('stats' => 0), $id);

        if ($res || $res === 0) {


            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }


    }

    /**
     * 列表页
     * @return mixed
     */
    public function index()
    {

        return $this->fetch('index');
    }

    public function getAllType()
    {
        $model = new \app\admin\model\Newstype();
        $w = array();
        $lists = $model->getDataForWheres($w);
        return $lists;
    }


    public function lists()
    {

        $ser_key = trim(input("request.ser_key"));//auth
        $ser_val = trim(input("request.ser_val"));//auth
        $ser_ty = trim(input("request.ser_ty"));//auth
        $ser_stats = trim(input("request.ser_stats"));//auth

        if ($ser_key && $ser_val) {
            $where[$ser_key] = array('like', '%' . $ser_val . '%');
        }
        if ($ser_ty > 0) {
            $where['ty'] = $ser_ty;
        }
        if ($ser_stats == '0' || $ser_stats == '1') {
            $where['stats'] = $ser_stats;
        }

        $starttime = trim(input("request.starttime"));//auth
        $endtime = trim(input("request.endtime"));//auth


        if (!empty($starttime) && !empty($endtime)) {
            $where['ctime'] = array('between', strtotime($starttime . " 00:00:00") . "," . strtotime($endtime . " 23:59:59"));
        }

        $lists = $this->_model->lists($where, $this->pagesize);
        $lists['data'] = changeUidToUserinfoForLists($lists['data'], 'uid');

        $this->assign('lists', $lists);


        $allType = $this->getAllType();
        $allType = changeArrayKey($allType, "id");
        $this->assign('allType', $allType);
        $this->assign('request', input("request."));
        return view('lists');

    }


    /**
     * 删除
     * 支持批量删除和单条删除
     * @return mixed
     */
    public function delData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }

        $userInfo = $this->_model->getID($id);
        $res = $this->_model->delData(array('id' => $id));

        if ($res) {

            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }


    }


    public function recordSystemLog($arr)
    {
        $model = new \app\admin\model\Log();
        $model->add($arr);
    }


    public function selectDeptChange()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $model = new \app\admin\model\Dep();
        $res = $model->getDataForWheres(
            array('father' => $id)
        );


        $return = ['err' => 0, 'msg' => '操作成功', 'data' => $res];
        return json($return);


    }


    public function addData()
    {

        $sys_opt = trim(input("request.sys_opt"));//auth
        if ($sys_opt) {
            $title = trim(input("request.title"));//auth
            $ty = trim(input("request.ty"));//auth
            $pic = trim(input("request.pic"));//auth
            $desc = trim(input("request.desc"));//auth
            $conts = trim(input("request.conts"));//auth
            $tuijian = trim(input("request.tuijian"));//auth

            $video = trim(input("request.video"));//auth
            $insertData = array(
                'title' => $title,
                'ty' => $ty,
                'pic' => $pic,
                'desc' => $desc,
                'conts' => $conts,
                'tuijian' => $tuijian,
                'video' => $video,
                'uid' => $this->LoginUserInfo['UserInfo']['id'],
                'ctime' => time()

            );
            $res = $this->_model->add($insertData);

            if ($res) {

                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }


        $allType = $this->getAllType();
        $this->assign('allType', $allType);
        //设置全局信息
        $title = "添加用户";
        $this->assign('title', $title);

        return view('addData');

    }


    public function infoData()
    {

        $id = trim(input("request.id"));//auth

        $info = $this->_model->getDataForWhere(array('id' => $id));

        $info['UserInfo'] = array();
        if ($info['uid']) {
            $info['UserInfo'] = getUserInfoForID($info['uid']);
        }

        $this->assign('info', $info);

        $allType = $this->getAllType();
        $allType = changeArrayKey($allType, "id");

        $this->assign('allType', $allType);
        //设置全局信息
        $title = "新闻详情";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('infoData');

    }


    public function editData()
    {

        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth
        //查询用户
        $info = $this->_model->getDataForWhere(array('id' => $id));

        if ($sys_opt) {
            $title = trim(input("request.title"));//auth
            $ty = trim(input("request.ty"));//auth
            $pic = trim(input("request.pic"));//auth
            $desc = trim(input("request.desc"));//auth
            $conts = trim(input("request.conts"));//auth

            $tuijian = trim(input("request.tuijian"));//auth

            $video = trim(input("request.video"));//auth
            $w = array(
                'id' => $id
            );
            $update = array(
                'title' => $title,
                'ty' => $ty,
                'pic' => $pic,
                'desc' => $desc,
                'conts' => $conts,
                'tuijian' => $tuijian,
                'video' => $video,
            );
            $res = $this->_model->updateDataForWhere($update, $w);

            if ($res || ($res === 0)) {

                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }

        $this->assign('info', $info);


        $allType = $this->getAllType();
        $this->assign('allType', $allType);
        //设置全局信息
        $title = "编辑新闻";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('editData');

    }


}